package cloud.shyidong.tenant.controller;

import cloud.shyidong.core.Result;
import cloud.shyidong.tenant.request.TenantInfoUpdatePasswordRequest;
import cloud.shyidong.tenant.request.TenantInfoUpdateRequest;
import cloud.shyidong.tenant.service.SelfService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * @Description TODO
 * @Author syd
 * @Date 2023/8/25
 */
@Slf4j
@RestController
public class SelfController {
    
    private SelfService tenantAuthService;
    
    @Autowired
    public void setTenantAuthService(SelfService tenantAuthService) {
        this.tenantAuthService = tenantAuthService;
    }
    
    /**
     * 查询登录信息
     *
     * @return
     */
    @GetMapping("/get")
    public Result get() {
        return tenantAuthService.getAuthInfo();
    }
    
    /**
     * 查询租户认证信息
     *
     * @return
     */
    @GetMapping("/tenant")
    public Result getTenant() {
        return tenantAuthService.getTenant();
    }
    
    /**
     * 查询菜单树
     *
     * @return
     */
    @GetMapping("/trees")
    public Result trees() {
        return tenantAuthService.getTreeList();
    }
    
    /**
     * 登录用户更新信息
     *
     * @param tenantInfoUpdateRequest
     */
    @PostMapping("/update")
    public Result update(@RequestBody @Valid TenantInfoUpdateRequest tenantInfoUpdateRequest) {
        if (log.isDebugEnabled()) {
            log.debug("tenant auth update param >>> {}", tenantInfoUpdateRequest);
        }
        return tenantAuthService.update(tenantInfoUpdateRequest);
    }
    
    /**
     * 登录用户修改密码
     *
     * @param tenantInfoUpdatePasswordRequest
     */
    @PostMapping("/update-pwd")
    public Result updatePassword(@RequestBody @Valid TenantInfoUpdatePasswordRequest tenantInfoUpdatePasswordRequest) {
        if (log.isDebugEnabled()) {
            log.debug("tenant auth updatePassword param >>> {}", tenantInfoUpdatePasswordRequest);
        }
        return tenantAuthService.updatePassword(tenantInfoUpdatePasswordRequest);
    }
    
    /**
     * 登录用户退出系统
     */
    @GetMapping("/logout")
    public Result logout() {
        return tenantAuthService.logout();
    }
    
}
